package com.l9lou.creeper.tools;

import java.io.IOException;

/**
 * User: eric
 * Date: 11-7-17
 * Time: 下午2:17
 */
public class ProtobufCompiler {

    public static void main(String...args) throws IOException {

        if(args.length == 0 || args.length != 3 ){
            usage();
        }

        if(args.length == 3){
            String pathToProtoc = args[0];
            String pathToProtoFile = args[1];
            String protoPath = "--proto_path=" + pathToProtoFile.replaceAll("\\s*\\w+\\.\\w+\\s*", "");
            String pathToGeneratedFiles = "--java_out=" + args[2];
            Runtime runtime = Runtime.getRuntime();
            String[] commands = {pathToProtoc, protoPath, pathToGeneratedFiles, pathToProtoFile};
            runtime.exec(commands);
        }

    }

    public static void usage(){
        StringBuilder sb = new StringBuilder();
        sb.append("Usage for protobuf compiler:\n");
        sb.append("java ProtobufCompiler /path/to/protoc /path/to/protofile.proto /path/to/outputPath \n");
        sb.append("when you omit the path to protoc,the program will search the protoc\n");
        sb.append("from user.dir");
        System.out.println(sb);
        System.exit(0);
    }

}
